﻿using System;
using System.Net;
using System.Net.Mail;
using System.Text.RegularExpressions;

namespace EastFantasy.DnfAccountScan.Public
{
    public static class PublicClass
    {

        #region 正则处理字符串
        /// <summary>
        /// 正则处理字符串 
        /// </summary>
        /// <param name="srcString">字符串</param>
        /// <param name="code">1:字符串只保留汉字 2:字符串移除所有汉字 3:判断是否是数字 4:提取字符串中的IP</param>
        /// <returns></returns>
        public static string RegexString(string srcString, int code)
        {
            switch (code)
            {
                case 1:
                    srcString = Regex.Replace(srcString, @"\D", "");
                    break;
                case 2:
                    srcString = Regex.Replace(srcString, @"\u4e00-\u9fa5", "");
                    break;
                case 3:
                    if (Regex.IsMatch(srcString, @"\d"))
                        srcString = "Y";
                    else
                        srcString = "N";
                    break;
                case 4:
                    Regex reg = new Regex(@"(?:25[0-5]|2[0-4]\d|1\d{0,2}|[1-9]\d?)\.(?:(?:25[0-5]|2[0-4]\d|1\d{0,2}|\d{1,2})\.){2}(?:25[0-5]|2[0-4]\d|1\d{0,2}|\d{1,2})(:(?<port>\d{0,4}))?");
                    MatchCollection collection = reg.Matches(srcString);
                    srcString = collection.Count > 0 ? collection[0].ToString() : string.Empty;
                    break;
            }
            return srcString;
        }
        #endregion

        #region 判断是否有中文字
        /// <summary>
        /// 判断是否有中文字
        /// </summary>
        /// <param name="srcString"></param>
        /// <returns>有,True||无,False</returns>
        public static bool CheckEncode(string srcString)
        {
            int strLen = srcString.Length;
            //字符串的长度，一个字母和汉字都算一个  
            int bytLeng = System.Text.Encoding.UTF8.GetBytes(srcString).Length;
            //字符串的字节数，字母占1位，汉字占2位,注意，一定要UTF8  
            //如果字符串的长度比字符串的字节数小，当然就是其中有汉字啦
            bool chkResult = strLen < bytLeng;
            return chkResult;
        }
        #endregion
    }

}
